package org.itboys.mobile.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayNews;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/7/17
 * Description: 集楼_新闻_web_service
 */

@Service
public class SundayNewsService extends BaseMongoService<SundayNews> {
    @Autowired
    private SundayUploadService uploadFileService;
    @Autowired
    private SundayActivityRelationService activityRelationService;
    @Autowired
    private SundayProductRelatedService productRelationService;
    @Resource(name = "mobileDs")
    private MongoDataSource kitchenDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<SundayNews> getEntityClass() {
        return SundayNews.class;
    }

    /**
     * 保存新闻
     *
     * @param news
     * @param imageFile
     * @throws Exception
     */
    public void saveNews(SundayNews news, MultipartFile imageFile) throws Exception {
        //保存新闻图片
        Map<String, String> map = uploadFileService.uploadSingle(imageFile);
        if (StringUtils.isNotEmpty(map.get(SundayUploadService.UPLOAD_HALF_PATH))) {
            news.setdefaultImage(map.get(SundayUploadService.UPLOAD_HALF_PATH));
        }
        //保存
        if (news.getId() == 0L) {
            news.setIsRecommend(0);
            super.save(news);
        } else {
            super.updateExceptEmpty(news.getId(), news);
        }
    }

    /**
     * 批量查询新闻
     * 可根据标题来
     *
     * @param request
     * @return
     */
    public PageResult<SundayNews> select(HttpServletRequest request) {
        Map<String, String> containsparam = ParamUtil.packageMongoVagueParam(request, "title");
        Map<String, Object> param = new HashMap<>();
        param.put("isDeleted", 0);
        PageResult<SundayNews> result = super.containpageQuery(request, containsparam, param);
        return result;
    }

    /**
     * 找出具体新闻
     *
     * @param id
     * @return
     */
    public SundayNews findOne(Long id) {
        SundayNews news = null;

        if (LongUtil.isNotZreo(id)) {
            news = super.getById(id);
        } else {
            news = new SundayNews();
        }
        return news;
    }

    /**
     * 设置是否首页推荐
     *
     * @param id
     * @param isRecommend
     */
    public void setStatus(Long id, Integer isRecommend) {
        super.update(id, "isRecommend", isRecommend);
    }


    public void delete(Long newsId) {
        super.delete(newsId);
        //同时删除相关的楼卡
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("otherId", newsId);
        param.put("type", 3);
        activityRelationService.delete(param);
        param.clear();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("otherId", newsId);
        param.put("type", 1);
        productRelationService.delete(param);
    }
}
